<template>
  <view class="goods-item">
    <!-- 商品左侧图片区域 -->
    <view class="goods-item-left">
      <!-- #1 :checked="goods.goods_state" 并绑定点击事件 -->
      <radio :checked="goods.goods_state" color="#C00000" v-if="showRadio" @click="radioClickHandler"></radio>
      <image class="goods-image" :src="goods.goods_small_logo || defaultPic"></image>
    </view>
    <!-- 商品右侧信息区域 -->
    <view class="goods-item-right">
      <!-- 商品标题 -->
      <view class="goods-name">{{goods.goods_name}}</view>
      <!-- 商品价格 -->
      <!-- #2 使用过滤器 -->
      <!-- <view class="goods-price">{{goods.goods_price | toFixed}}</view> -->
      <view class="goods-price">{{toFixed2(goods.goods_price) }}</view>
      <uni-number-box v-if="showNum" :min="1" :value="goods.goods_count" @change="numChangeHandler"></uni-number-box>
    </view>
  </view>
</template>

<script>
  export default {
    name: "my-goods",
    data() {
      return {
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
    props: {
      showRadio: {
        type: Boolean,
        default: false
      },
      showNum: {
        type: Boolean,
        default: false
      },
      goods: {
        type: Object,
        // Vue 中指定默认值如果是复杂数据类型，要是通过函数再返回这个复杂数据类型
        default: () => ({})
      }
    },
    // 本质就是对数据进行处理的一个函数
    filters: {
      // #1 定义过滤器的名字
      toFixed(v) {
        // v 就是使用过滤器的时候,管道符左边的内容
        return Number(v).toFixed(2)
      }
    },
    methods: {
      toFixed2(v) {
        // v 就是使用过滤器的时候,管道符左边的内容
        return Number(v).toFixed(2)
      },
      radioClickHandler() {
        // #2 触发外界的自定义事件的同时并传参
        this.$emit('radio-change', {
          goods_id: this.goods.goods_id, // 为了知道要改谁
          goods_state: !this.goods.goods_state // 我要改为什么状态
        })
      },
      numChangeHandler(num) {
        this.$emit('num-change', {
          goods_id: this.goods.goods_id, // 为了知道要改谁
          goods_count: num // 我要改为什么
        })
      }
    },
  }
</script>

<style lang="scss">
  .goods-item {
    display: flex;
    border-bottom: 1px solid #efefef;
    padding: 8rpx;

    .goods-item-left {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .goods-image {
        width: 200rpx;
        height: 200rpx;
        vertical-align: middle;
      }
    }

    .goods-item-right {
      display: flex;
      flex: 1;
      flex-direction: column;
      justify-content: space-between;
      padding-left: 10px;
      line-height: 20px;

      .goods-name {
        font-size: 13px;
      }

      .goods-info-box {
        display: flex;
        align-items: center;
        justify-content: space-between;
      }

      .goods-price {
        font-size: 16px;
        color: #c00000;
      }
    }
  }
</style>
